#include "scerespondmanager.h"

SceRespondManager* 		SceRespondManager::soleInstance = NULL;

SceRespondManager::~SceRespondManager()
	{
	listObserver.Reset();
	listObserver.Close();
	
	listRespond.Reset();
	listRespond.Close();
	}
    
SceRespondManager::SceRespondManager()
	{
	}
	
SceRespondManager& SceRespondManager::GetInstance()
	{
	if (soleInstance == NULL)
		soleInstance = new SceRespondManager;
	
	return *soleInstance;
	}
	
SceRespondManager::RespondItem* SceRespondManager::GetRespond() const
	{
    int size = listRespond.Count();
    if(size > 0)
    {
        RespondItem* respondItem = listRespond[size - 1];
        listRespond.Remove(size-1);
        return respondItem;
    }
    return NULL;
	}
	
void SceRespondManager::AddRespond(int id, int nCmd, const TDesC8& arrayOfByte, const TDesC8& string)
	{
    RespondItem* respond = new RespondItem();
    respond->id = id;
    respond->ncmd = nCmd;
    respond->data = arrayOfByte.Alloc();
    respond->codingType = string;

    listRespond.Insert(respond, 0);
	}

void SceRespondManager::DoOnData(const SceRespondManager::RespondItem& respond)
	{
	if (listObserver.Count() > 0)
		{
        ISceRespondObserver* localISceRespondObserver = (ISceRespondObserver*) listObserver[0];
        localISceRespondObserver->OnData(respond.id, respond.ncmd,*respond.data, respond.codingType);
		}
	}
	
void SceRespondManager::RegisterObserver(ISceRespondObserver& iSceRespondObserver)
	{
    TBool bool1 = listObserver.Find(&iSceRespondObserver) != KErrNotFound;
    if (!bool1)
    {
        listObserver.Append(&iSceRespondObserver);
    }
	}

void SceRespondManager::UnregisterObserver(ISceRespondObserver& iSceRespondObserver)
	{
	TInt idx;
    TBool bool1 = (idx = listObserver.Find(&iSceRespondObserver));
    if (bool1)
    {
        listObserver.Remove(idx);
    }
	}

